package cn.gailvlun.gll.presentation.wish.drawer;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import cn.gailvlun.gll.Constants;
import cn.gailvlun.gll.R;
import cn.gailvlun.gll.chatlib.db.ChatDB2;
import cn.gailvlun.gll.databinding.ActivityWishDetailBinding;
import cn.gailvlun.gll.net.HttpMethods;
import cn.gailvlun.gll.net.HttpSubscriber;
import cn.gailvlun.gll.net.base.Profile;
import cn.gailvlun.gll.net.wish.OperationRecordReq;
import cn.gailvlun.gll.net.wish.Wish;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.presentation.chat.ChatActivity;
import cn.gailvlun.gll.presentation.wish.WishType;
import cn.gailvlun.gll.util.DialogUtil;
import cn.gailvlun.gll.util.RxUtil;
import cn.gailvlun.gll.widget.NavigationBarView;
import io.reactivex.functions.Consumer;
import io.realm.Realm;
import rx_activity_result2.Result;
import rx_activity_result2.RxActivityResult;


public class WishDetailActivity extends BaseActivity {

    private ActivityWishDetailBinding mBinding;
    private int mWishType;

    public static void openActivity(Activity activity, Wish item, @WishType int wishType, Consumer<Result<Activity>> resultConsumer) {
        Intent intent = new Intent(activity, WishDetailActivity.class);
        
        String wishString = Wish.stringFromObj(item);
        intent.putExtra(Constants.EXTRA_WISH_OBJ, wishString);
        intent.putExtra(Constants.EXTRA_WISH_TYPE, wishType);
        RxActivityResult.on(activity)
                .startIntent(intent)
                .subscribe(resultConsumer);
    }

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(mContext, R.layout.activity_wish_detail);
        mBinding.setPresenter(new Presenter());
        Intent intent = getIntent();
        String wishSting = intent.getStringExtra(Constants.EXTRA_WISH_OBJ);
        Wish wish = Wish.objectFromData(wishSting);
        mWishType = intent.getIntExtra(Constants.EXTRA_WISH_TYPE, -1);
        mBinding.setItem(wish);
    }

    @Override
    protected void createHandler() {
        mBinding.navigationBar.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
    }

    public class Presenter {

        public void onPostComment(int wishType, Wish item) {
            if (wishType == mWishType) {
                CommentActivity.openActivity((Activity) mContext, wishType, item.getId(), new Consumer<Result<Activity>>() {
                    @Override
                    public void accept(Result<Activity> activityResult) throws Exception {
                        finish();
                    }
                });
            }

        }

        public void onReviewComment(int wishType, Wish item) {
            CommentDetailActivity.openActivity(mContext, item);
        }

        public void onChatWith(int wishType, Wish wish) {
            Realm realm = Realm.getInstance(ChatDB2.INSTANCE.getConfiguration());
            realm.beginTransaction();

            Profile accepter = wish.getAccepter();
            Profile owner = wish.getOwner();
            if (wishType == Constants.OWNER) {
                realm.insertOrUpdate(accepter);
                ChatActivity.openActivity(mContext, "user:" + accepter.getId());
            } else if (wishType == Constants.ACCEPTER) {
                realm.insertOrUpdate(owner);
                ChatActivity.openActivity(mContext, "user:" + owner.getId());
            }
            realm.commitTransaction();
            realm.close();
            finish();
        }

        public void onRealized(Wish item) {
            OperationRecordReq req = new OperationRecordReq()
                    .setWish_id(item.getId())
                    .setAction("finish");
            HttpMethods.getWishService().realized(req)
                    .compose(RxUtil.<Wish>applyScheduler())
                    .subscribe(new HttpSubscriber<Wish>() {
                        @Override
                        protected void onSuccess(Wish response) {

                            setResult(RESULT_OK);
                            finish();
                        }

                        @Override
                        protected void onFailure(String errMsg, @Nullable Wish response, int code) {
                            DialogUtil.showError(mContext, errMsg);
                        }
                    });
        }


        public int getRealizingVisibility(int type, Wish item) {
            
            if (type == mWishType && item.getStatus() == 2) {
                return View.VISIBLE;
            } else {
                return View.GONE;
            }

        }


        public int getRealizedVisibility(int type, Wish item) {
            
            if (type == mWishType && item.getStatus() == 3) {
                return View.VISIBLE;
            } else {
                return View.GONE;
            }

        }

    }

}
